﻿<%@ WebHandler Language="C#" Class="GetProducts" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using BusinessObjects.BO_GetObjects;

public class GetProducts : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string Module = context.Request.QueryString["module"];

        DataTable dt = BO_Products.SelectByModule(1, 1, 10, Convert.ToInt32(Module));
        StringBuilder strArticles = new StringBuilder();

        if (dt != null)
        {
            strArticles.Append("{ Products:[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                decimal price = Convert.ToDecimal(dt.Rows[i]["UNIT_PRICE"].ToString());
                string strPrice = "";
                if (price > 1000000)
                {
                    int dTemp = 0;
                    dTemp += Convert.ToInt32(price / 1000000);
                    strPrice += dTemp + " Triệu ";
                    if (price - Convert.ToDecimal(dTemp * 1000000) > 0)
                    {
                        strPrice += (price - Convert.ToDecimal(dTemp * 1000000)) / 1000;
                    }
                }

                strArticles.Append("{");
                strArticles.Append("\"Index\":\"" + (i + 1) + "\",");
                strArticles.Append("\"ProductID\":\"" + dt.Rows[i]["PRODUCT_ID"].ToString() + "\",");
                strArticles.Append("\"Name\":\"" + dt.Rows[i]["PRODUCT_NAME"].ToString() + "\",");
                strArticles.Append("\"Description\":\"" + dt.Rows[i]["PRODUCT_DESCRIPTION"].ToString() + "\",");
                strArticles.Append("\"Thumb\":\"" + dt.Rows[i]["THUMB_IMAGE"].ToString() + "\",");
                strArticles.Append("\"RegionName\":\"" + dt.Rows[i]["REGION_NAME_VN"].ToString() + "\",");
                strArticles.Append("\"Price\":\"" + strPrice + "\"");
                if (i != dt.Rows.Count - 1)
                {
                    strArticles.Append("},");
                }
            }
        }
        strArticles.Append("}");
        strArticles.Append("]}");
        context.Response.ContentType = "application/json";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        context.Response.Write(strArticles.ToString());
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}